﻿using CDSysLDManageProject.BYDTableAdapters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class FmRecipeSet : Form
    {
        tb_DCIRRecipeInfoTableAdapter recipinfo = new tb_DCIRRecipeInfoTableAdapter();
        ServTimeGetTableAdapter sertime = new ServTimeGetTableAdapter();
        public string CheckStatus = "";

        public FmRecipeSet()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            dgvprocess.AutoGenerateColumns = false;
            dgvprocess.AllowUserToAddRows = false;
        }

        private void FmRecipeSet_Load(object sender, EventArgs e)
        {
            LoadRecipeData();
        }

        public void LoadRecipeData()
        {
            DataTable dtrecipeinfo = recipinfo.GetData();
            dgvprocess.DataSource = dtrecipeinfo;

        }

        private void btnnew_Click(object sender, EventArgs e)
        {
            try
            {
                dgvprocess.Rows.Add(0);
            }
            catch (Exception)
            {

                MessageBox.Show("请先完成上一行的编辑!");
            }
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            string flag = "recipeset";
            FormClosedCheck formcheck = new FormClosedCheck(flag);
            formcheck.Owner = this;
            formcheck.ShowDialog();

            if (CheckStatus != "OK")
            {
                MessageBox.Show("请输入正确密码!");
                return;

            }


            for (int i = 0; i < dgvprocess.RowCount; i++)
            {

                if (dgvprocess.Rows[i].Cells["recipeid"].Value == null)
                {
                    //MessageBox.Show("请输入完整的工艺信息!");
                    continue;
                }
                if (dgvprocess.Rows[i].Cells["recipeid"].Value.ToString() == "")
                {
                    MessageBox.Show("请输入电源对应的工艺类型!");
                    return;
                }

                //不存在KID则insert
                if (dgvprocess.Rows[i].Cells["keyid"].Value.ToString() == "0")
                {
                    DateTime dt = DateTime.Parse(sertime.ScalarQuery().ToString());
                    try
                    {
                        recipinfo.Insert(dgvprocess.Rows[i].Cells["traytype"].Value.ToString(), dgvprocess.Rows[i].Cells["recipeid"].Value.ToString());
                        //pinfo.InsertProcessType(dgvprocess.Rows[i].Cells["processtype"].Value.ToString(), DateTime.Parse(sertime.ScalarQuery().ToString()), null, null);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("将正常工艺设置数据存到数据库失败!");
                        Log.SQL.Add("将正常工艺设置数据存到数据库失败" + ex.ToString());
                    }

                }//否则看是否修改
                else
                {
                    string ProcessCount = recipinfo.GetRecipeCountByRecipeIdTrayTypeKid(dgvprocess.Rows[i].Cells["recipeid"].Value.ToString(), dgvprocess.Rows[i].Cells["traytype"].Value.ToString(), int.Parse(dgvprocess.Rows[i].Cells["keyid"].Value.ToString())).ToString();
                    if (int.Parse(ProcessCount) > 0)
                    {

                    }
                    else
                    {

                        DateTime dt = DateTime.Parse(sertime.ScalarQuery().ToString());
                        try
                        {
                            recipinfo.UpdateRecipeIdByKid(dgvprocess.Rows[i].Cells["recipeid"].Value.ToString(), int.Parse(dgvprocess.Rows[i].Cells["keyid"].Value.ToString()));

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("将正常工艺设定信息同步到数据库失败!");
                            Log.SQL.Add("将正常工艺设定信息同步到数据库失败" + ex.ToString());
                        }


                    }
                }

            }
            MessageBox.Show("保存成功!");
            this.Close();
            
        }

        
    }
}
